package AddProduct;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ChooseBrandAndProducer
 * This servlet gives option to customer to choose brand and producer of new product.
 * @author desso
 */
@WebServlet("/ChooseBrandAndProducer")
public class ChooseBrandAndProducer extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ChooseBrandAndProducer() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 try {	
		    	SetDataInSession(request);
				Connection databaseConnection = SQL.getConnection();
				ResultSet  resultSetForProducer = SQL.getDataFromDatabase(databaseConnection, SQLStatements.STATMENT_FOR_PRODUCERS);
				ResultSet  resultSetForBrands = SQL.getDataFromDatabase(databaseConnection, SQLStatements.STATMENT_FOR_BRANDS);
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				PrintWriter prinWriter = response.getWriter();
				prinWriter.println("<!DOCTYPE html>");
				prinWriter.println("<html>");
				prinWriter.println("   <head>");
				prinWriter.println("<meta charset=\"UTF-8\">");
				prinWriter.println("   </head>");
				prinWriter.println("    <body>");
				prinWriter.println("      <h3> Please choose producer and brand of the new product! </h3>");		
				prinWriter.println("      <form name=\"new product\" action=\"InsertIntoItems\" method=\"post\">");			
				HTML.printSelect(prinWriter, resultSetForProducer, "producer");			
				HTML.printSelect(prinWriter, resultSetForBrands, "brand");
				prinWriter.println("         <input type=\"submit\" value=\"Continue\">");	
				prinWriter.println("       </form> ");
				prinWriter.println("    </body>");
				prinWriter.println("</html>");

			} catch (SQLException | ClassNotFoundException e) {
				e.printStackTrace();
			}		
		}

		private void SetDataInSession(HttpServletRequest request) {
			HttpSession sess = request.getSession(true);
			sess.setAttribute("description", request.getParameter("description"));
			sess.setAttribute("price", request.getParameter("price"));
			sess.setAttribute("promotional price", request.getParameter("Promotional price"));
			sess.setAttribute("whole sale price", request.getParameter("Whole sale price"));
		}
}
